const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    mindate:Math.round((new Date() - 365*86400*1000)),
    limit: 5,
    count: 0,
    flag: true, 
    select:false,
    date:"",
    datastatus:0,//查看 0全部/1准备中/2进行中/3未完成 
    datafind:"",//搜索关键字
    show:false,
    findtime:"",//时间范围查找
    production_complete_time_text:"",//生产完成时间,状态为4时显示
    delivery_rate :"",//项目整体交货进度 状态为3时显示
    production_rate:"",//项目整体生产进度，状态为2时显示
    status:"",//项目状态

  },
  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    this.getList(e.detail)
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },
  
  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },

  goPage(event){
    let tmp = this.data.list[event.currentTarget.dataset.index].p_no
    wx.navigateTo({
      url: `/pages/addinfo/addinfo?p_no=${tmp}`,
    })
  },
  onDel(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      success: function (res) {
        if (res.confirm) {
          // 确定操作
          let params = {
            p_no:that.data.list[e.currentTarget.dataset.index].p_no
          }
          request.requestGet('project/del', params, "加载中",function (res) {
            if (res.status === 200) {
              that.getList()
              } else {
                that.setData({
                  flag:false
                })
              }
            }, function (res) {
              console.log(res,"请求失败")
            },
          )
        } else if (res.cancel) {
          // 取消操作
        }
      }
    })
  },
  onSelect(e){
    var data = this.data.list[e.currentTarget.dataset.index];
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    //页面功能定义
    var invoice = {
      id:data.id,
      currency:data.currency,
      invoice_no:data.invoice_no,
      fpze:data.fpze,
      currency:data.currency
    };
    prevPage.setData({
      invoice:invoice
    })
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.select && options.select!=undefined){
        var select = options.select>=1?true:false;
        this.setData({select:select})
      }
      this.data.page = 1
      this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // console.log("下拉")
    // that.setData({
    //     page: that.data.page + 1,
    //     flag:true
    //   })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 2
    }else if(event.detail.index === 0){
      this.data.datastatus = 0
    }else if(event.detail.index === 3){
      this.data.datastatus = 3
    }else if(event.detail.index === 4){
      this.data.datastatus = 4
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  getList(search="",date="") {
    var that = this;
    var params = {
      page: that.data.page,//当前页码
      type: this.data.datastatus,//是否完成
      keyword:this.data.datafind,//关键字
      add_time:this.data.findtime,//时间范围
      limit: that.data.limit,
      search:this.data.datafind,
      select:Number(that.data.select),
      date:date
    }

    //页面功能定义
    request.requestGet('order/get_project_list', params, "加载中",function (res) {
      console.log(res,"resssssssss")
      if (res.status === 200) {
        let list = that.data.list
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          that.setData({
            flag:false
          })
        }
        that.setData({
          list: list,
          count: res.data.count
        })
      }
      console.log(that.data.list,"listttttttttttttt")

      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },
  projectinfo(event){
    let tmp = this.data.list[event.currentTarget.dataset.index].p_no
    wx.navigateTo({
      url: `/pages/projectinfo/projectinfo?p_no=${tmp}`,
    })
  },
  del_and_add(e){
    let p_no = e.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/del_raw_materials/del_raw_materials?p_no=${p_no}`,
    })
  },
  start_work(e){
    var that = this

    let params = {
      p_no : e.currentTarget.dataset.index
    }
    request.requestGet('project/startProduction', params, '开始生产',function (res) {
      if (res.status === 200) {
        that.getList()
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    }, function (res) {
      wx.showToast({
        title: '失败',
        icon:'none'
      })
    })
  },
  look_purchase_orders(e){
    let p_no = e.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/look_purchase_orders/look_purchase_orders?p_no=${p_no}`,
    })
  },
  to_delivery(e){
    let p_no = e.currentTarget.dataset.index

    wx.navigateTo({
      url: `/pages/to_delivery/to_delivery?p_no=${p_no}`,
    })
  },


  delivery_warning(e){
        let p_no = e.currentTarget.dataset.index
        this.data.selstart = 2
        wx.navigateTo({  url: `/pages/to_delivery/to_delivery?p_no=${p_no}` })
    },
})